<?php
function dmodel_new_object($query,$data=array()) {
	$a = dmodel_get_args($query,"class");	
	$c = dmodel_get_class($a->class);	
	$p = dmodel_get_prefix($a);

	$d = new stdClass();
	$d->form = $a->class;
	$d->mood = 1;
	$d->born = date("Y-m-d H:i:s");
	$d->uniq = isset($a->uniq) ? $a->uniq : md5(rand(1000,9999).time());
	$d->root = isset($a->root) ? $a->root : 0;
	$d->name = isset($a->name) ? $a->name : "";
	
	if (!dmodel_hit_object("{$d->form}&uniq={$d->uniq}")) {
		foreach($c->form as $n=>$f) {
			if (false) {
			} else if (dmodel_is_object_standard_field($f["class"])) {
				if (isset($data[$f["field"]])) {
					$d->{$f["class"]} = $data[$f["field"]];
				} else if (isset($data[$n])) {
					$d->{$f["class"]} = $data[$n];
				}
				
			} else if (dmodel_is_object_meta_field($f["class"])) {				
				#do nothing
				
			} else if (dmodel_is_object_class_field($f["class"])) {
				#do nothing
				
			}
		}		
		$i = dmodel_sql_insert($d,"object");
		$q = "INSERT INTO {$p}object({$i->fields}) VALUES ({$i->values})";
		//oh_dump($q);
		$r = dmodel_query($q,"lastid");	
		//oh_dump($r);
		#store meta
		foreach($c->form as $n=>$f) {
			if ($f["class"]=="id") {
				#do nothing
				
			} else if (dmodel_is_object_standard_field($f["class"])) {							
				#do nothing
				
			} else if (dmodel_is_object_meta_field($f["class"])) {				
				if (isset($data[$f["field"]])) {
					dmodel_set_meta($r,$f["field"],$data[$f["field"]]);
				} else if (isset($data[$n])) {
					dmodel_set_meta($r,$f["field"],$data[$n]);					
				} 
				
			} else if (dmodel_is_object_class_field($f["class"])) {
				if (is_array($data[$f["field"]])) {
					foreach($data[$f["field"]] as $item) {
						if (is_array($item)) {
							$id = dmodel_new_object($f["class"]."&root={$r}",$item);
							dmodel_new_meta($r,$f["field"],$id);
						} 						
					}
				} else if (isset($data[$f["field"]])) {				
					dmodel_set_meta($r,$f["field"],$data[$f["field"]]);
				}				
			}
		}				
		return $r;	
	}
}

function dmodel_get_object($query,$data=array()) {
	$a = dmodel_get_args($query,"class");	
	$p = dmodel_get_prefix($a);

	if (isset($a->id)) {
		if ($a->id==0) {
			return dmodel_get_object_default($query);	
		}
	}
	if ($a->get=="default") {
		return dmodel_get_object_default($query);
	}
	if ($a->get=="model") {
		oh_dump("AAA");
		return "";
	}	
	
	$w[] = "mood>0";
	$w[] = $a->class ? "form LIKE '{$a->class}'" : "form!=''";
	$w[] = isset($a->id) ? "id={$a->id}" : "id>0";
	$w[] = isset($a->root) ? "root={$a->root}" : "root=0";
	$w = implode(" AND ",$w);
		
	$q = "SELECT * FROM {$p}object WHERE {$w} ";
	if (isset($a->id)) {
		$r = dmodel_query($q,"record");
		dmodel_object_decoration($r);
		dmodel_object_meta_decoration($r);
		dmodel_object_evaluate_decoration($r);
		return $r;
	} else {
		$r = dmodel_query($q,"resultset");
		if (count($r)>0) {
			foreach($r as &$i) {
				dmodel_object_decoration($i);
				dmodel_object_meta_decoration($i);
				dmodel_object_evaluate_decoration($i);
			}
		}
		return $r; 
	}	
}

function dmodel_hit_object($query,$data=array()) {
	$a = dmodel_get_args($query,"class");	
	$c = dmodel_get_class($a->class);	
	$p = dmodel_get_prefix($a);

	$d = new stdClass();
	$d->form = $a->class;
	$d->uniq = $a->uniq;
	
	$q = "SELECT * FROM {$p}object WHERE form='{$d->form}' AND uniq='{$d->uniq}' LIMIT 1";
	
	$r = dmodel_query($q);	
	return $r;
}

function dmodel_del_object($query,$data=array()) {
	$a = dmodel_get_args($query,"class");	
	$c = dmodel_get_class($a->class);	
	$p = dmodel_get_prefix($a);

	$d = new stdClass();
	$d->form = $a->class;
	//$d->uniq = $a->uniq;
	
	$q = "DELETE FROM {$p}object WHERE id={$a->id} LIMIT 1";	
	$r = dmodel_query($q);	
	
	return $r;
}

function dmodel_object_decoration(&$r) {
	if (is_object($r)) {		
		$c = dmodel_get_class($r->form);
		foreach($c->form as $f) {
			if (dmodel_is_object_standard_field($f["class"])) {
				$r->{$f["field"]} = isset($r->{$f["class"]}) ? $r->{$f["class"]} : "";				
			}
		}
		$r->form = $c->form;
	}
}

function dmodel_object_meta_decoration(&$r) {
	if (is_object($r)) {
		if (is_array($r->form)) {
			$c = new stdClass();
			$c->form = $r->form;
		} else {
			$c = dmodel_get_class($r->form);	
		}
		foreach($c->form as $f) {
			if ($f["field"]=="id") {
			} else if ($f["array"] == "yes") {
				$r->{$f["field"]} = dmodel_get_object($f["class"]."&root={$r->id}");
				
			} else if (dmodel_is_object_meta_field($f["class"])) {
				$r->{$f["field"]} = dmodel_get_meta($r->id,$f["field"]);
				
			} else if (dmodel_is_object_class_field($f["class"])) {			
				$r->{$f["field"]} = dmodel_get_meta($r->id,$f["field"]);
				if (((int)$r->{$f["field"]})>0) {
					$r->{$f["field"]} = (int)$r->{$f["field"]};
					$r->{$f["field"]} = dmodel_get_object($f["class"]."&id=".$r->{$f["field"]});
				}
				
			} 
		}	
	}
}

function dmodel_object_evaluate_decoration(&$r) {
	if (is_object($r)) {
		if (is_string($r->form)) {
			$r->form = dmodel_get_class($r->form);
		}
		foreach($r->form as $f) {
			$r->{$f["field"]} = ($f["parse"]) ? dmodel_evaluate($f["field"],$r->{$f["field"]},$f["space"],$f["depes"],$r) : $r->{$f["field"]};
		}
	}
}

function dmodel_evaluate($field,$value,$space,$depes,&$object) {
	if (count($depes)>0) {
		$repl = array();
		foreach($depes as $d) {
			$math = new EvalMath();			
			if (strpos($d,".")>0) {
				$t = explode(".",$d);				
				if (is_array($object->{$t[0]})) {
					$capt = array();
					foreach($object->{$t[0]} as $o) {
						$capt[] = $o->{$t[1]};
					}
					$repl[$d] = implode(",",$capt);
				}
				
			} else {
				$eval = $d.' = '.((int)($object->$d));			
				$math->e($eval);	
				if ($math->last_error) {
					oh_dump("EVAL: ".$eval."\nERROR: ".$math->last_error);
					die();
				}						
			}
		}
		$return = array();
		foreach($space as $s) {
			if ($s) {
				foreach($repl as $find=>$with) {
					$s = str_replace($find,$with,$s);
				}
				$return[] = $math->e($s);
				if ($math->last_error) {
					oh_dump("EVAL: ".$s."\nERROR: ".$math->last_error);
					die();
				}
			}
		}
		return implode(",",$return);			
	} else {
		return $value;
	}	
}

function dmodel_get_object_default($query) {
	$a = dmodel_get_args($query,"class");	
	$c = dmodel_get_class($a->class);	
	$p = dmodel_get_prefix($a);
	$q = "SHOW COLUMNS FROM {$p}object";
	$r = dmodel_query($q);	
	
	$d = new stdClass();

	if (count($r)>0) {	
		foreach($r as $f) {
			$d->{$f->Field} = "";
		}
		$d->form = $a->class;
		dmodel_object_decoration($d);
		dmodel_object_meta_decoration($d);
		dmodel_object_evaluate_decoration($d);
	}
	
	return $d;
}


